package com.sakura.test1;


import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.downloader.Downloader;
import us.codecraft.webmagic.processor.PageProcessor;

public class TaobaoPageProcessor implements PageProcessor {

    private Taobao taobao = new Taobao();

    // 抓取网站的相关配置，包括：编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public Site getSite() {
        return site;
    }

    @Override
    public void process(Page page) {
//        page.addTargetRequests(page.getHtml().links()
//                .regex(".*item\\.taobao\\.com/item\\.htm\\?id=.*")
//                .all());
//        page.addTargetRequests(page.getHtml().links()
//                .regex("https://s\\.taobao\\.com/list.*")
//                .all());
//
//        //如果是详情页
//        if(page.getUrl().regex("https://item\\.taobao\\.com/item\\.htm\\?id=.*").match()) {
//
//            Taobao taobao = new Taobao();
//            taobao.setId("1");
//            taobao.setUrl(page.getUrl().toString());
//            taobao.setMaintitle(page.getHtml().xpath("//h3[@class='tb-main-title']/text()").get());
//            taobao.setSubtitle(page.getHtml().xpath("//p[@class='tb-subtitle']/text()").get());
//            taobao.setPrice(page.getHtml().xpath("//strong[@id='J_StrPrice']/em[@class='tb-rmb-num']/text()").get());
//            taobao.setTaobaoprice(page.getHtml().xpath("//em[@id='J_PromoPriceNum']/text()").get());
//            taobao.setRatecounter(page.getHtml().xpath("//strong[@id='J_RateCounter']/text()").get());
//            taobao.setSellcounter(page.getHtml().xpath("//strong[@id='J_SellCounter']/text()").get());
//            System.out.println(taobao.toString());
//        }
        String title = page.getHtml().xpath("/html/body/div[8]/div/div[2]/div[1]/text()").get();
        String tbrmb = page.getHtml().xpath("/html/body/div[8]/div/div[2]/div[3]/div/div[1]/div[2]/span[1]/span[2]/text()").get();
        String pj = page.getHtml().xpath("//*[@id=\"comment-count\"]/a/text()").get();
        System.out.println("打印标题：" + title);
        System.out.println("打印京东价格：" + tbrmb);
        System.out.println("评价数量：" + pj);
    }

    public static void main(String[] args) {
        Spider.create(new TaobaoPageProcessor()).setDownloader(new Downloader() {
            
            @Override
            public void setThread(int threadNum) {
            }
            
            @Override
            public Page download(Request request, Task task) {
                return GetAjaxHtml.download(request);
            }
        }).addUrl("https://item.jd.com/27182642439.html").thread(5).run();
    }
}